<?php
namespace Admin\Controller;

class GoodsController extends BaseController {
    
    public function index(){
        
        // 获取所有商品栏目
        $cats = M('category')->select();
        
        $cats_str = '';
        foreach ($cats as $v){
            $cats_str .= '{ id:'.$v['id'].', pId:'.$v['pid'].', name:"'.$v['cat_name'].'",path:"'.U('goodsList',array('cat_id'=>$v['id'])).'"},';
        }
        
        // 传递数据
        $this->assign('cats_str',$cats_str);
        
        $this->display();
        
    }
    
    // 添加修改商品
    public function goodsAdd(){
        // 获取数据模型
        $cat_m = M('category');
        $brand_M = M('brand');
        $goods_M = D('Goods');
        
        // 获取所有栏目
        $cats = M('category')->field('id,pid,cat_name')->select();
        $cats = threeData($cats);
        
        $this->assign('cats',$cats);
        
        // 获取栏目id
        $cat_id = I('get.cat_id');
        
        // 获取栏目名称
//         $cat_name = $cat_m->where(array('id'=>$cat_id))->getField('cat_name');
        
        // 获取所有品牌
        $brands = $brand_M->field('id,brand_name')->select();
        
        // 传递数据
        $this->assign('cat_name',$cats);
        $this->assign('brands',$brands);
        $this->assign('cat_id',$cat_id);
        
        if (IS_POST){
           
            // 检测数据
            $data = $goods_M->create(I('post.'));
            
            if (!$data){ // 验证不通过
                $this->ajaxReturn(array('status'=>'n','msg'=>$goods_M->getError()));
            }
            
            if(I('post.goods_id')>0){
                
                unset($data['goods_id']);
                $res = $goods_M->where(array('id'=>I('post.goods_id')))->save($data);
            }else{
                $data['addtime'] = time();
                // 添加数据
                $res = $goods_M->add($data);
            }
            
            if ($res !== false){
                $this->ajaxReturn(array('status'=>'y'));
            }else {
                $this->ajaxReturn(array('status'=>'n','msg'=>'失败'));
            }
        }elseif(I('get.goods_id') > 0){
            
            // 获取商品id
            $goods_id = I('get.goods_id');
            
            // 获取商品数据
            $goods = M('goods')->find($goods_id);
            
            // 传递数据
            $this->assign('goods',$goods);
            $this->display();
        }else {
            
            $this->display();
        }
        
    }
    
    // 显示商品列表
    public function goodsList(){
        $good_M = M('goods');
        // 获取商品栏目id
        $cat_id = I('get.cat_id');
        
        // 获取所有字栏目
        $cats = M('category')->field('id,pid,cat_name')->select();
        
        $three_cats = threeData($cats,$cat_id);
        $cat_ids = array();
        foreach ($three_cats as $v){
            $cat_ids[] = $v['id'];
        }
        $cat_ids[] = $cat_id;
        
        
        // 获取当前栏目下的商品
        $goods = $good_M->where(array('cat_id'=>array('in',$cat_ids)))->select();
        
        // 传递数据
        $this->assign('cat_id',$cat_id);
        $this->assign('goods',$goods);
        $this->assign('cats',$cats);
//         dump($cats);
//         dump($goods);die;
        $this->display();
    }
    
    // 删除商品
    public function goods_del(){
        
        // 获取商品id
        $goods_id = I('get.goods_id');
        
        // 删除商品
        $res = M('goods')->delete($goods_id);
        
        if ($res !== false){
            
            // 删除该商品下的图片
            $images = M('goods_photo')->where(array('goods_id'=>$goods_id))->select();
            
            if (!empty($images)){
                foreach ($images as $v){
                    @unlink(trim($v['thumb_image'],'/'));
                    @unlink(trim($v['big_image'],'/'));
                    @unlink(trim($v['mid_image'],'/'));
                    @unlink(trim($v['image'],'/'));
                }
            }
            
            echo 1;die;
        }else{
            echo 0;die;
        }
    }
    
    // 搜索商品栏目
    public function searchCat(){
        
        // 获取栏目模型
        $cat_M = M('category');
        
        $cat_name = trim(I('post.cat_name'));
        
        // 查询栏目
        if ($cat_name != ''){
            $res = $cat_M->where(array('cat_name'=>array('like',"%$cat_name%")))->select();
        }
        $html = '';
        if (!empty($res)){
            foreach ($res as $v){
                $html .= '<li><a href="'.U('goodsList',array('cat_id'=>$v['id'])).'">'.$v['cat_name'].'</a></li>';
            }
        }
        
        echo $html;die;
    }
    
    // 修改商品是否上架
    public function changeSale(){
        // 获取商品id
        $goods_id = I('post.goods_id');
        
        $goods_M = M('goods');
        
        // 获取当前商品的状态
        $status = $goods_M->where(array('id'=>$goods_id))->getField('is_on_sale');
        
        if ($status == 1){
            $res = $goods_M->where(array('id'=>$goods_id))->setField('is_on_sale',0);
        }else {
            $res = $goods_M->where(array('id'=>$goods_id))->setField('is_on_sale',1);
        }
        
        if ($res !== false){
            if ($status == 1){
                echo 0;die;
            }else {
                echo 1;die;
            }
        }else {
            echo 'error';die;
        }
    }
}
?>